package com.haonan.springboot_exercise.util;

/**
 * @author haonan
 * @date 2021/5/16 19:05
 **/
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class SmsCode {
    public static String getSms(String sPhoneNumbers) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","LTAI4GD29Ht9pMVY2mhHEzev","0KYNSeDcEUXaCwqYuYbhNnIhFsqzS2");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", sPhoneNumbers);
        request.putQueryParameter("SignName", "渔小助");
        request.putQueryParameter("TemplateCode", "SMS_205892237");
        String code = Integer.toString(((int)(Math.random()*9000+1000)));
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return code;
    }
}
